<!-- delete confirmation dialog box -->
<div id="delConfDialog" title="Confirm">
    <p>Anda yakin?</p>
</div>
<!-- message dialog box -->
<div id="msgDialog"><p></p></div>
<!--
<script type="text/template" id="readTemplate">
        <tr id="${handset_id}">
                <td width="30px">${nomor}</td>
                <td>${handset_model}</td>
		<td>${handset_man}</td>
		<td>${user_name}</td>
		<td>${imei}</td>
		<td>${imsi}</td>
		<td>${msisdn}</td>
                <td width="110px"><a class="updateBtn" href="${updateLink}">Perbarui</a> | <a class="deleteBtn" href="${deleteLink}">Hapus</a></td>
        </tr>
</script>
-->

<!-- edited by : alfin akhret
SSP datattables -->
<table id="records" class="handsetRecord">
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>
<!--  -------------->

<script type="text/javascript">
var readUrl   = '<?=base_url();?>index.php/handset/read',
    updateUrl = '<?=base_url();?>index.php/handset/update',
    delUrl    = '<?=base_url();?>index.php/handset/delete',
    delHref,
    updateHref,
    updateId;


$( function() {
    
    readUsers();

    $( '#msgDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
            }
        }
    });
    
    $( '#delConfDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'No': function() {
                $( this ).dialog( 'close' );
            },
            'Yes': function() {
                //display ajax loader animation here...
                $( '#ajaxLoadAni' ).fadeIn( 'slow' );
                $( this ).dialog( 'close' );
                $.ajax({
                    url: delHref,
                    success: function( response ) {
                        //hide ajax loader animation here...
                        $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                        
                        $( '#msgDialog > p' ).html( response );
                        $( '#msgDialog' ).dialog( 'option', 'title', 'Success' ).dialog( 'open' );
                        
                        $( 'a[href=' + delHref + ']' ).parents( 'tr' )
                        .fadeOut( 'slow', function() {
                            $( this ).remove();
                        });
                        
                    } //end success
                });
            } //end Yes
        } //end buttons
    }); //end dialog
    
    
    $( '#records' ).delegate( 'a.deleteBtn', 'click', function() {
        delHref = $( this ).attr( 'href' );
        $( '#delConfDialog' ).dialog( 'open' );
            return false;
    }); //end delete delegate
    // --- Create Record with Validation ---
}); //end document ready


// function readUsers() {
//     //display ajax loader animation
//     $( '#ajaxLoadAni' ).fadeIn( 'slow' );
    
//     $.ajax({
//         url: readUrl,
//         dataType: 'json',
//         success: function( response ) {
//             var j=1;
//             for( var i in response ) {
//                 response[ i ].updateLink = updateUrl + '/' + response[ i ].handset_id;
//                 response[ i ].deleteLink = delUrl + '/' + response[ i ].handset_id;
//                 response[ i ].nomor = j;
//                 j++;
//             }
//             //clear old rows
//             $( '#records > tbody' ).html( '' );
            
//             //append new rows
//             $( '#readTemplate' ).render( response ).appendTo( "#records > tbody" );
            
//             //apply dataTable to #records table and save its object in dataTable variable
//             if( typeof dataTable == 'undefined' )
//                 dataTable = $( '#records' ).dataTable({"bJQueryUI": true});
            
//             //hide ajax loader animation here...
//             $( '#ajaxLoadAni' ).fadeOut( 'slow' );
//         }
//     });

    function readUsers()
    {
       
        $('.handsetRecord').dataTable({
                "bJQueryUI": true, // aktifkan tampilan tabel JQueryUI
                "sAjaxSource": readUrl, //data source
                "sAjaxDataProp": "aaData", //variabel yg menampung data table
                "bServerSide": true, //serverside processing true
                "bProcessing": true, // tampilkan 'processing...' status saat data dipanggil
                //"sPaginationType": 'fullNumbers',
                "bPaginate": true,
                "aoColumns":[ //tentukan kolom pd tabel yg akan menampung value dari server
                    {"mDataProp": "handset_id", "bVisible":false, "bSearchable":false}, 
                    {"mDataProp": "handset_model", "sTitle": "Model Handset", "sWidth": "25%"}, 
                    {"mDataProp": "handset_man", "sTitle": "Pabrikan", "sWidth": "15%"}, 
                    {"mDataProp": "user_name", "sTitle": "Pengguna", "sWidth": "20%"}, 
                    {"mDataProp": "imei", "sTitle": "ISN", "sWidth": "15%"}, 
                    {"mDataProp": "imsi", "sTitle": "IMSI", "sWidth": "25%"},
                    {"mDataProp": "msisdn", "sTitle": "MDN", "sWidth": "35%"}
                    //{"mDataProp": null, "sDefaultContent": "Edit", "sTitle": "Action", "aTargets": [ -1 ]}

                ],
                "fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) 
                {
                    //$(nRow).find('td').eq(0).before('<td>'+iDisplayIndex+'</td>');

                    $(nRow).find('td').eq(-1).after('<td><a href="'+updateUrl+'/'+aaData[0]+'">Update</a> | <a href="'+delUrl+'/'+aaData[0]+'" class="deleteBtn">Delete</a></td>');
                    return nRow;
                }
        });

        $('.handsetRecord thead tr').find('th').eq(-1).after('<th class="ui-state-default" width="10%">Action</th>');
        // $('.handsetRecord thead tr').find('th').eq(0).before('<th class="ui-state-default">No</th>');
    
} // end readUsers
</script>
